﻿<?php
if(!defined('IN_PHPSCUP'))exit('hacking'); 
class RollpicApp extends BaseApp
{
    function index()
    {
		
		$GLOBALS['Lang']->_get_lang('rollpic');	
		
		$TB_rollpic = &$this->_model('rollpic');  
		
		$picdata = $TB_rollpic->show();
        $this->assign('picdata',$picdata);		 

	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('rollpic_list.view.html');
    }

    function edit()
    {
		
	    $id = $_GET['id'];	
		
		$GLOBALS['Lang']->_get_lang('rollpic');	
		
		$TB_rollpic = &$this->_model('rollpic');  
		
		$picdata = $TB_rollpic->oneshow($id);
        $this->assign('picdata',$picdata);	
				
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('rollpic_edit.view.html');
    }

    function add()
    {
		
	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('rollpic_add.view.html');
    }


    function editpost()
    {

	     $data = $_POST['data'];
		 unset($data['path']);
		 
		 $id = intval($data['id']);	 
		 $TB_rollpic = &$this->_model('rollpic');		 		 		
		 $picdata = $TB_rollpic->oneshow($id);
		 
		if($_FILES['postfile1']['tmp_name'])
	    { 			 
	     include(ROOT_PATH.'/lib/upfile.lib.php');	  
	     $upfile = new Upfile('rollpic'); 	  
	     $updata = $upfile->_batch_upfile('rollpic');
		
		 if($picdata['images_url'] && !empty($updata))
		 {
		 $pic_dir = ROOT_PATH . '/' . UPFILE_PATH . '/' . $picdata['images_url'];
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		               del_file($pic_dir);	
		              }				 
		 }	 
		 if(empty($updata))
		 {
		 $data['path'] = $picdata['images_url'];		 
		 }
		 else
		 {
		 $data['path'] = $updata[1]['path'];		 
		 }
		}
		 if(!isset($data['path']))$data['path']=$picdata['images_url'];	
		 $TB_rollpic->editaction($id,$data);
		 
		 $this->write_cache();
		 
		 $this->index();

    }

    function addpost()
    {

	     $data = $_POST['data'];
		 unset($data['path']);
		if($_FILES['postfile1']['tmp_name'])
	    { 			 
	     include(ROOT_PATH.'/lib/upfile.lib.php');	  
	     $upfile = new Upfile('rollpic'); 	  
	     $updata = $upfile->_batch_upfile('rollpic');		 
		 if(!isset($updata[1]['path']))	
		 {
		 $updata[1]['path'] ='';
		 }	 
		 $data['path'] = $updata[1]['path'];
		 }
		 if(!isset($data['path']))$data['path']='';		 
		 $TB_rollpic = &$this->_model('rollpic'); 
	 
		
		 $TB_rollpic->post($data);
		 
		 $this->write_cache();
		 
		 $this->index();

    }

    function del()
    {

	     $id = $_GET['id'];
		 $TB_rollpic = &$this->_model('rollpic');
		 $picdata = $TB_rollpic->oneshow($id);		 		

		 if($picdata['images_url'])
		 {
		 $pic_dir = ROOT_PATH . '/' . UPFILE_PATH . '/' . $picdata['images_url'];
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		               del_file($pic_dir);	
		              }			 
		 }

		 
		 $TB_rollpic->del($id);	
		 	 
		 $this->write_cache();		 
		 $this->index();	  

    }

    function psort()
    {
		 $data = array();
		 
	     $id = $_GET['id'];
		 $data['sort'] = $_GET['v'];
		 
		 $TB_rollpic = &$this->_model('rollpic');		 		
		 $picdata = $TB_rollpic->editsort($id,$data); 
		 $this->write_cache();		 
		 $this->index();			  

    }


    function cache()
    {
         
		 $this->write_cache();		 
		 $this->index();		 


    }


    function write_cache()
    {
         
		 
		$TB_rollpic = &$this->_model('rollpic');  		
		$picdata = $TB_rollpic->show();
		
		array_var_sort($picdata,'sort');
		
		foreach($picdata as $key => $value)
		{
		 unset($picdata[$key]['id'],$picdata[$key]['sort'],$picdata[$key]['add_time']);		  
		
		}
		
		$cache_filename = ROOT_PATH . '/data/cache/rollpic_'.trim($GLOBALS['Lang']->_lang_tp).'.php';
		
	    write_file($cache_filename, "<?php if(!defined('IN_PHPSCUP'))exit('hacking');  \nreturn " . var_export($picdata , true) . ";\n?>");
		
		if(!is_writable($cache_filename))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }


    }


 
  
}







?>